<template>
	<view class="setting">
		<view class="info">
			<view class="photo" @click="putPhoto">
				<!-- <view class="photo"> -->
				<view class="title">头像</view>
				<view class="">
					<image v-if="userInfo.avatar!=''" :src="userInfo.avatar" mode=""></image>
					<image v-else src="../../static/icon/pic.png" mode=""></image>
					<image src="../../static/icon/next_.png" mode=""></image>
				</view>
			</view>
			<view class="user-info" v-for="(item, index) in infoList" :key="index" @click="amend(index)">
				<view class="">{{ item.title }}</view>
				<view class="">
					<view class="">{{ item.content }}</view>
					<view class="modify">
						<image src="../../static/icon/next_.png" mode=""></image>
					</view>
				</view>
			</view>
			
			<!-- <view class="user-info" @click="switcher()">
				<view class="">切换账号</view>
				<view class="">
					<view class="">去切换</view>
					<view class="modify">
						<image src="../../static/icon/next_.png" mode=""></image>
					</view>
				</view>
			
			</view> -->
			<view class="btn" @click="exit">退出登录</view>
		</view>

		<view class="">
			<u-modal v-model="show" :show-cancel-button="true" confirm-text="确认" title="退出登录？" @cancel="cancel" @confirm="confirm"
			 confirm-color="#f81149">
				<view class="u-update-content">
					<rich-text :nodes="content"></rich-text>
				</view>
			</u-modal>
		</view>
	</view>
</template>

<script>
	import service from '@/store/service.js';
	export default {
		data() {
			return {
				show: false,
				avatar: '',
				content: '确认退出登录吗?',
				user: '',
				userInfo: '',
				infoList: [{
						title: '昵称',
						content: ''
					},
					{
						title: '手机号码',
						content: '去修改'
					},
					/* {
						title: '当前账号',
						content: '去修改'
					}, */
					{
						title: '重置登录密码',
						content: '去修改'
					},
					// {
					// 	title: '修改交易密码',
					// 	content: '去修改'
					// }
				]
			};
		},
		onShow() {
			let self = this
			self.getUserInfo();
		},
		methods: {
			switcher(){
				uni.navigateTo({
					url:'/pages/setting/switch'
				})
			},
			getUserInfo() {
				var self = this;
				self.$http.post(service.api.user.info, {}).then(function(res) {
					// console.log('1111',res);
					var list = res.data;
					self.userInfo = list;

					self.infoList[0].content = list.nickname;
					self.infoList[1].content = list.mobile;
					//self.infoList[2].content = list.account;
				});
			},
			jump(url) {
				uni.navigateTo({
					url: url
				});
			},
			amend(index) {
				// if(index != 0){
				// 	this.jump(`./amend?id=${index}`)
				// }
				this.jump(`./amend?id=${index}`);
			},
			//退出登录
			exit() {
				this.show = !this.show;
			},
			//取消退出登录
			cancel() {
				
			},
			//确认退出登录
			confirm() {
				let that = this;
				// this.$http.post(this.$api.api.user.logout).then(res => {
				// console.log('退出登录');
				// if (res.code === 1) {
				// that.$store.commit('logout');
				uni.removeStorage({
					key: 'token'
				});
				this.$api.msg('退出成功');
				setTimeout(() => {
					this.$jump('/pages/login/login');
				}, 1500);
				// } else {
				// 	this.$api.msg(res.info);
				// }
				// });
			},
			//更换头像
			putPhoto() {
				let that = this;
				console.log('更换');
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'],
					sourceType: ['album'],
					success: function(res) {
						console.log(res);
						uni.showLoading({
							title: '加载中'
						});
						if (res.errMsg == 'chooseImage:ok') {
							console.log('ok');
							that.avatar = res.tempFilePaths[0];
							let image = res.tempFilePaths[0];
							that.$http.upload(service.api.user.upload + '?token=' + uni.getStorageSync('token'), res.tempFilePaths[0],
								'image').then(function(res) {
								uni.hideLoading();
								uni.showToast({
									icon: 'none',
									title: '头像上传成功'
								});
								if (res.code === 1) {
									//修改头像
									that.$http
										.post(service.api.user.updateInfo, {
											avatar: res.data.img_url
										})
										.then(res => {
											console.log('上传头像====>>', res);
											that.$http.post(service.api.user.info).then(res => {
												// console.log(res,111);
												if (res.code === 1) {
													// console.log('更新===>>', res);
													that.userInfo = res.data;
													// uni.setStorageSync('userInfo', res.data);
													// that.$store.state.userInfo = res.data;
													uni.hideLoading();
													that.$api.msg('更新成功');
												}
											});
										});
								}
							});
							// });
							// that.$http.upload(that.$api.api.common.upload, res.tempFilePaths[0], 'image').then(i => {
							// 	console.log('上传图片===>>', i);
							// 	if (i.code === 1) {
							// 		//修改头像
							// 		that.$http
							// 			.post(that.$api.api.my.modify_info, {
							// 				avatar: i.data.img_url,
							// 				nickname: ''
							// 			})
							// 			.then(res => {
							// 				console.log('上传头像====>>', res);
							// 				that.$http.get(that.$api.api.user.userInfo).then(res => {
							// 					if (res.code === 1) {
							// 						console.log('更新===>>', res);
							// 						uni.setStorageSync('userInfo', res.data);
							// 						that.$store.state.userInfo = res.data;
							// 						uni.hideLoading();
							// 						that.$api.msg('上传成功');
							// 					}
							// 				});
							// 			});
							// 	}
							// });
						}
					}
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	@import '../../common/uni.css';

	page {
		width: 100%;
		height: 100%;
		overflow: hidden;

		.setting {
			height: 100%;
			overflow: auto;
			background-color: #f4f4f4;
		}
	}

	.info {
		margin-top: 30upx;

		.photo {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 20upx 30upx;
			background-color: #fff;
			color: #333333;

			&>view:nth-child(2) {
				display: flex;
				align-items: center;
				// width: 128upx;
				// height: 128upx;

				>image:nth-child(1) {
					width: 89upx;
					height: 89upx;
					border-radius: 50%;
				}

				>image:nth-child(2) {
					width: 48upx;
					height: 48upx;
				}
			}
		}

		.user-info {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 30upx;
			border-top: 2upx solid #f1f1f1;
			background-color: #fff;
			color: #333333;

			&>view:nth-child(2) {
				display: flex;
				align-items: center;
				color: #999999;
				font-size: 28upx;
			}

			.modify {
				width: 48upx;
				height: 48upx;

				image {
					width: 100%;
					height: 100%;
				}
			}
		}

		.btn {
			width: 630upx;
			height: 100upx;
			line-height: 100upx;
			background: #f81149;
			border-radius: 50upx;
			margin: 0 auto;
			text-align: center;
			color: #fff;
			font-size: 36upx;
			margin-top: 200upx;
		}
	}

	.margin {
		margin-top: 30upx;
	}

	.amend {
		.content {
			background-color: #ffffff;
			margin-top: 30upx;
			padding: 0 30upx;

			.box {
				padding: 40upx 0;
				border: 1upx solid#F4F4F4;
				color: #333333;
			}

			.code {
				display: flex;
				align-items: center;
				justify-content: space-between;

				>view {
					width: 124upx;
					height: 52upx;
					line-height: 52upx;
					text-align: center;
					border-radius: 26upx;
					background-color: #5670fe;
					font-size: 28upx;
					color: #ffffff;
				}
			}
		}

		.btn {
			width: 630upx;
			height: 100upx;
			line-height: 100upx;
			text-align: center;
			border-radius: 50upx;
			color: #ffffff;
			background-color: #5670fe;
			font-size: 36upx;
			margin: 60upx auto 0 auto;
		}
	}

	//uview样式
	.u-update-content {
		font-size: 28upx;
		color: #666666;
		text-align: center;
		padding: 20upx 0;
	}

	/deep/.u-model-title {
		font-size: 40upx;
		font-weight: bold;
		color: #333333;
	}
</style>
